package com.gxa.system_setting.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.gxa.common.R;
import com.gxa.common.entity.system_settings.SensitiveWords;
import com.gxa.system_setting.service.SensitiveWordsService;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.ibatis.annotations.Options;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
public class SensitiveWordsController {

    @Autowired
    private SensitiveWordsService sensitiveWordsService;

    //分页
    @GetMapping("/SensitiveWordsList")
    @SentinelResource(value = "SensitiveWordsList", blockHandler = "blockHandler",fallback = "fallBackList")
    public R  SensitiveWordsList(Integer currentPage,Integer pageSize){
        int i=1/0;
        return sensitiveWordsService.SensitiveWordsList(currentPage,pageSize);
    }
    //限流
    public R blockHandler(Integer currentPage, Integer pageSize, BlockException e){
            R r=new R();
            r.setCode(500);
            r.setMessage("限流");
            return r;
    }
    //调用方式时出现错误调用的方法
    public R fallBackList(Integer currentPage, Integer pageSize, Throwable e){
        R r=new R();
        r.setCode(500);
        r.setMessage("回滚");
        return r;
    }


    //编辑
    @PostMapping("/updateSensitiveWords")
    public R updateSensitiveWords(@RequestBody SensitiveWords sensitiveWords){
        return sensitiveWordsService.updateSensitiveWords(sensitiveWords);
    }


    //添加
    @Options(useGeneratedKeys = true,keyProperty = "sensitiveWordId",keyColumn = "sensitive_word_id")
    @PostMapping("/insertSensitiveWords")
    //开启事务
    @GlobalTransactional(name = "updateSensitiveWords",rollbackFor = Throwable.class)
    public R insertSensitiveWords(@RequestBody SensitiveWords sensitiveWords){
        R r = sensitiveWordsService.insertSensitiveWords(sensitiveWords);
        r.setCode(200);
        r.setMessage("添加完成");
        return r;
    }


    //条件查询
    @RequestMapping("/selectByConditions")
    public R selectByConditions(@RequestBody Map map){
        return sensitiveWordsService.selectByConditions(map);
    }


    //批量修改状态
    @RequestMapping("/batchUpdateStatus")
    public R batchUpdateStatus(Long[] sensitive_word_ids,Integer status){
        return sensitiveWordsService.batchUpdateStatus(sensitive_word_ids,status);
    }


    //批量删除
    @RequestMapping("/batchDelSensitiveWordsList")
    public R batchDelSensitiveWordsList(Long[] sensitive_word_ids){
        return sensitiveWordsService.batchDelSensitiveWordsList(sensitive_word_ids);
    }

}
